package com.woniu.sharepark.leasesystem.common.service.impl;
import com.woniu.sharepark.common.JsonResult;
import com.woniu.sharepark.common.pojo.Login;
import com.woniu.sharepark.constant.SystemConstant;
import com.woniu.sharepark.leasesystem.common.dao.CarParkCommentDAO;
import com.woniu.sharepark.leasesystem.common.dao.CarParkCommentReplyDAO;
import com.woniu.sharepark.common.dao.UserInfoDAO;
import com.woniu.sharepark.leasesystem.common.pojo.CarParkCommentReply;
import com.woniu.sharepark.leasesystem.common.service.CarParkCommentService;
import com.woniu.sharepark.leasesystem.common.pojo.CarParkComment;
import com.woniu.sharepark.util.UploadImageUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 *
 *@ClassName CarParkCommentImpl
 *@Description 车位评论模块接口的实现类
 *@Author litong
 *@Date 2019/6/3 20:02
 *@Version 1.0
 **/
@Service
public class CarParkCommentImpl implements CarParkCommentService {

    @Value("${file.path}")
    String path;
    @Value("${file.address}")
    String address;
    @Autowired
    private CarParkCommentDAO carParkCommentDAO;
    @Autowired
    private UserInfoDAO userInfoDAO;
    @Autowired
    private CarParkCommentReplyDAO carParkCommentReplyDAO;
    @Override
    public List<CarParkComment> findCommentByPage(Integer currentPage, Integer pageSize,Integer cId) {
        return carParkCommentDAO.queryCommentByPage(currentPage,pageSize,cId);
    }

    @Override
    public JsonResult addComment( CarParkComment carParkComment, MultipartFile files, HttpServletRequest request) throws IOException {
    if (carParkComment.getCpcContent()==null||carParkComment.getCpcContent().equals("")){
        JsonResult.error("评论内容不能为空");
    }
    Session session  = SecurityUtils.getSubject().getSession();
    Login login = (Login) session.getAttribute("currentUser");
    int uId=login.getUserInfo().getuId();
    String uName=userInfoDAO.selectUNameByUId(uId);
       // System.out.println(uName);
    //设置评论方姓名
    carParkComment.setuName(uName);
    carParkComment.setuId(uId);
         //保存图片
       String picName;
        try {
           picName = UploadImageUtil.uploadImage(files);
        } catch (IOException e) {
            e.printStackTrace();
            return JsonResult.error("图片上传出错");
        }
        carParkComment.setCpcPic(picName);
        carParkComment.setCpcTime(new Date());
        carParkCommentDAO.addComment(carParkComment);
        return JsonResult.ok("评论成功");
    }

    @Override
    public JsonResult deleteComment(Integer cpcId) {
        carParkCommentReplyDAO.deleteAllReply(cpcId);
        carParkCommentDAO.deleteComment(cpcId);
        return JsonResult.ok("撤回成功");
    }

    @Override
    public JsonResult findAllCommentsAndReply(Integer cId) {

        List<CarParkComment>list=new ArrayList<>();
        list=carParkCommentDAO.queryAllComments(cId);
        for (CarParkComment comment:list){
            int cpcId=comment.getCpcId();
            List<CarParkCommentReply>replies=carParkCommentReplyDAO.queryReplysByCpcId(cpcId);
            comment.setReplies(replies);
        }
        return new JsonResult("Success", SystemConstant.SUCCESS_CODE,list);
    }

    @Override
    public List<CarParkComment> selectScoreCount(String cIds) {
        return carParkCommentDAO.selectScoreCount(cIds);
    }


}
